package util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;

public class CatchDataUtil {

	private static Logger LOG = Logger.getLogger(CatchDataUtil.class);
	private static HttpClient client;
	
	public static String getWebSite(String webURL) {
		if(UsualUtil.emptyOrNot(webURL)) {
			StringBuffer webSite = new StringBuffer();
			String webSizeRegex = "^http://(.*?)/.*?";
			Pattern pattern = Pattern.compile(webSizeRegex);
			Matcher matcher = pattern.matcher(webURL);
			if(matcher.find())	webSite.append(matcher.group(1));
			return webSite.toString().trim();
		}
		LOG.info("webURL is empty !");
		return null;
	}
	
	public static HttpClient initHttpClient() {
		if (client == null) {
			MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
			client = new HttpClient(connectionManager);
			// 配置服务器参量
			client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);// 配置Cookie策略为浏览器默认策略
			client.getParams().setParameter(
					"http.protocol.single-cookie-header", true);
			client.getParams().setParameter(
					HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
			client.getHttpConnectionManager().getParams().setConnectionTimeout(
					300000);
			client.getHttpConnectionManager().getParams().setSoTimeout(3000000);
		}
		return client;
	}
	
	public static void setHeaders(HttpMethod method, String webURL, String host) {
		method.getParams().setParameter("http.protocol.cookie-policy",
				CookiePolicy.BROWSER_COMPATIBILITY);
		method.setRequestHeader("accept",
						"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		method.setRequestHeader("referer", webURL);
//		method.setRequestHeader("accept-encoding","gzip,deflate,sdch");
		method.setRequestHeader("accept-language", "zh-CN,zh;q=0.8");
		method.setRequestHeader("content-type","application/x-www-form-urlencoded");
		method.setRequestHeader("user-agent",
						"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9VbHRpbWF0ZS5leGUmbWFjPUY4RDgxQUY2MTgwQjQ3REQ5MUI5QkM4NjU3MzAwRkU3JnBhc3Nwb3J0PSZ2ZXJzaW9uPTIwMTAuNi4zLjYuMiZjcmFzaHR5cGU9MQ==&verify=b226e2d473bb8dbbb37568db14fee0e4; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9TcGVjaWFsLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAxMC42LjMuNS4yJmNyYXNodHlwZT0x&verify=94c65611a34cfb4964638346b072d76e; staticlogin:product=cboxf09&act=login&info=ZmlsZW5hbWU9UG93ZXJ3b3JkMjAwOU94Zi4yNTI2OS40MDExLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAwOS4wNS4yNS4zLjI3MiZjcmFzaHR5cGU9MQ==&verify=32e210faa8ce04f90dfc4225f98e80ac; aff-kingsoft-ciba; TheWorld)");
		method.setRequestHeader("host", host);
		method.setRequestHeader("connection", "Keep-Alive");
		method.setRequestHeader("cache-control", "no-cache");
//		method.setRequestHeader("cookie", "JSESSIONID=AAD4A68D4746D8214874716DD5DEC418");
	}
	
	public static void saveDataFile(String sFolder, String data, long sid, String encode) {
		File folder = new File(sFolder);
		if (!folder.exists()) folder.mkdirs();
		PrintWriter out = null;
		String saveFile = sFolder + "/" + sid + ".htm";
		try {
			out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
					saveFile), encode));
			out.println(data);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
					out = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			LOG.info("【" + DateGroup.getNow() + "】" + saveFile.replaceAll("\\\\", "/"));
		}
	}
	
	public static void saveFileAsTxt(String sFolder, String data, int sid, String encode) {
		File folder = new File(sFolder);
		if (!folder.exists()) folder.mkdirs();
		PrintWriter out = null;
		String saveFile = sFolder + "/" + sid + ".txt";
		try {
			out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
					saveFile), encode));
			out.println(data);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
					out = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			LOG.info("【" + DateGroup.getNow() + "】" + saveFile.replaceAll("\\\\", "/"));
		}
	}
	
	public static void writeLog(String log) {
		File txtFile = new File(Conts.HTM_ROOT_PATH +"catchLog.csv");
		FileWriter fw = null;
		try {
			fw = new FileWriter(txtFile,true);
			fw.append(log);
			fw.flush();
        } catch (IOException e) {
			LOG.info(e);
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				LOG.info(e);
			}
		}
	}
}
